# !/usr/bin/env python
# coding=utf-8
# @Time    : 2017/12/27 17:37
# @Author  : 6imensiva
# @Site    : https://gitee.com/dimensiva/wx_server_prototype.git
# @File    : ticket_service.py
# @Software: PyCharm
# @Version: 3.5.4
from app.dao import my_redis
from utils import my_request, my_exception
from configs import current_config


def get_jsapi_ticket(access_token):
	ticket = my_redis.get_data('jsapi_ticket')
	if ticket:
		return ticket

	url = current_config.JSAPI_TICKET_URL % (access_token)
	ret_dict = my_request.my_request4('GET', url, parse_format='json')
	if ret_dict.get('errcode') != 0:
		raise my_exception.MyException('fail to get ticket:'+ret_dict.get('errmsg'))
	ticket = ret_dict['ticket']
	my_redis.set_data('jsapi_ticket', ticket, ret_dict['expires_in'])
	return ticket